memory_alphafandomcom-20200223-history
Help:Parser function
A "parser function" is a -like construct which returns a value based on at least one unnamed parameter, separated from the function name by a colon ":'''"; there may be more parameters which, like those of ordinary templates, are each preceded by "|'". The syntax can be written with or without a leading (#): : for: *Core parser functions (standardly built into MediaWiki); this is for backwards compatibility; the function "language" is an exception; it is a core parser function, but in MessagesEn.php is specified that the wikitext name is "#language" anyway. *Optionally for parser function extensions. or : *By default for parser function extensions, e.g. ParserFunctions extension, to prevent collision with namespaces and interwiki prefixes. The distinction with variables is that the latter do not have any arguments, and also no colon. For each language there is a file like MessagesEn.php specifying for each variable and parser function whether the name is case-sensitive. Usually variables are case-sensitive and parser functions are not. To some variables ":R" can be added ("raw" modifier), but, although now a parser function, the name is still case-insensitive. Also the names DISPLAYTITLE and DEFAULTSORT are case-sensitive. MediaWiki's core parser functions are listed below and in . Formatting LC Makes a text lowercase and removes newlines and spaces from the start and end. yields . yields . UC Makes a text uppercase and removes newlines and spaces from the start and end. yields . yields , expected: Ä Β SS. Don't expect too much. LCFIRST Makes the first character lowercase and removes newlines and spaces from the start and end: yields . UCFIRST Makes the first character uppercase and removes newlines and spaces from the start and end: yields . URLENCODE Removes newlines and spaces from the start and end, and converts the remaining text into URL-friendly format by replacing spaces with "+" and other URL formatting characters with their escaped equivalents: yields . * } yields } * yields (with the old preprocessor not the percent code %3C of the symbol itself, but the percent code of the HTML code <) * yields (with the old preprocessor not the percent code %3E of the symbol itself, but the percent code of the HTML code >) * }} yields }} ("|" needs to be given indirectly, using Template:!) ANCHORENCODE For anchors ''within a page use instead of . The results of a call to are compatible with intra-page references generated for links, while -generated values are not necessarily so. NS Gives the namespace name for a given number or name: Also, for example, talk}} yields talk}}. NS:1 talk}} works as well: talk}}, as do the following constructs: yields , yields , yields , and }} also yields }}. Note that the function is not case sensitive. NS:0 # # yields # #, an empty string. This is often handy, less vulnerable than a template void or similar, and less cryptic than like __END__. In constructs like } | } | ns:0}} }}, where the parameter T''' is normally the name of a template to be evaluated and can be undefined or empty, is then a clean empty string. Without it would result in . Please note that }} fails: }}. NS cannot handle an empty argument. An empty section title like above is obtained using, in this case, . This is dubious, but arguably better than using the same trick to get invisble section headers twice on a page. At least /* */ makes sense in the edit history, if there's only one section using this particular trick. As shown in the table of contents this and similar tricks result in non-functional links. In conjunction with it's less harmful, and maybe useful to get edit links for invisible sections with categories and interlanguage links. Other possibilities for different invisible section headers: # # # # URLs, etc. Note that with the interwiki prefix the format "wiki/" is used even for edit pages etc., while it does not write the page name in canonical form. For interwiki links localurl and fullurl give the same: The parser function localurl: replaces spaces by underscores and special characters by escape codes, e.g. yields . The main purpose was to create a link to a given page in external link style in the same project or a project which uses the same string in the URL between the server name and the page name. This function is now better served by less verbose fullurl: constructs, is a shorthand for . Both fullurle: and localurle: performed additional character escaping on the resulting link, but no example is known where that still has any additional effect. Note that fullurl: and localurl: don't encode special characters and spaces in the optional query-string parameter. At least for spaces this will result in broken URLs: With target= &limit=10 as query-string it works: &limit=10}} The new urlencode: encodes spaces as "+'", not "'_'" or the generally supported "'%20'"; this might have interesting effects, so take care. Leading and trailing spaces are stripped, but all internal spaces are preserved: yields ' . For obvious reasons encoding "}}" or "|'" cannot work directly. Don't encode "'='", "'&'", or "';'" in structured ''query-strings unless you really want it. Example Say you want to create a link in a template to the block log for blocked usernames: " } }}". This won't work with spaces in the name like "Example blocked user" (unless they are manually underlined) as you'd get: " ", so what you can do is escape it with " }}} }}", giving you " }}". Also, the magic words for page name like , , etc. all have "escaped" versions: , , etc. #language: gives the local language name of selected RFC 3066 language codes, otherwise it returns the input value as is. For a complete list see the IANA registry. : yields : yields : yields : yields : yields : yields (Klingonese) : yields (missing, introduced 2006) : yields (no language code as of 2006) Use of result The result, like that of a variable or template, and like a template parameter value, can be used in the following ways: *the name of a template: }} *the name of a variable: }} *the name of a parser function: :EXAMPLE}} *the name of a template parameter: =Example}} *the value of a template parameter: }} *a template parameter default value: }}} *the name of a switch parameter: =A}} *the value of a parser function parameter: |example|yes}} *the target or label of an internal link: *the target or label of an external link: gives: *the name of a template: }} *the name of a variable: }} *the name of a parser function: :EXAMPLE}} *the name of a template parameter: =Example}} *the value of a template parameter: }} *a template parameter default value: }}} *the name of a switch parameter: =A}} *the value of a parser function parameter: |example|yes}} *the target or label of an internal link: *the target or label of an external link: The result cannot be used inside XML-style extension tags: * \over 3 gives \over 3 Expansion The first step in any wikitext is identifying the templates, parser functions, variables and parameters from the braces, pipes and semicolons. For a parser function the next step is expanding the wikitext for the name of the parser function. Suppose for example that it is #ifexpr. Then the next step is expanding the condition. Based on that either the wikitext for the then-part or that for the else-part is expanded. This is illustrated with | }} | }} giving | }} | }}. A page with this wikitext transcludes example only (with the old preprocessor also bad example). Interpretation of a parser function as a template In some cases a "wrong" parser function call is interpreted as a template call, which results in an entry in the templatelinks table, which in turn results in an entry in the list of pages included in a page (on the edit box of the source page) and of the target page. This applies, for example, for localurl: and fullurl: if the parameter is not a valid page name, while "Template:localurl:" and "Template:fullurl:" followed by the parameter is. This is the case if the parameter is a namespace prefix followed by a colon, such as: * yielding * yielding This entry in the templatelinks table occurs even if the result produced is not rendered due to a conditional parser function. This may occur in practice using, for example: * }|.. } }}..}} * }|.. } }}..}} in the case that the parameter is equal to the empty string; it does not occur if the parameter is undefined, because "Template:fullurl:Template: }" is not a valid page name. Thus, the list of templates transcluded in a page may be polluted with the entries of the non-existing template Template:Fullurl:Template: and similarly for other namespace names instead of the second "Template", and for "localurl" instead of "fullurl". One remedy is: * }|.. }| }|[]}} }}..}} because "Template:fullurl:Template:[]" is not a valid page name. Substitution Applying "subst:" to a parser function works: yields abc. Note that unless a technique like is used, substituting a template which uses a parser function does not replace that parser function with its result. See also Help: Substitution at MetaWiki and Manual: Substitution at MediaWiki. See also *Parser functions manual * * * *CoreParserFunctions.php *The plural function (also ) * , and